package cn.edu.njtc.controller;

import cn.edu.njtc.common.ErrorMessage;
import cn.edu.njtc.common.R;
import cn.edu.njtc.common.Records;
import cn.edu.njtc.entity.dto.*;
import cn.edu.njtc.entity.pojo.User;
import cn.edu.njtc.entity.pojo.UserDetail;
import cn.edu.njtc.entity.vo.UserVo;
import cn.edu.njtc.exception.BizException;
import cn.edu.njtc.service.UserDetailService;
import cn.edu.njtc.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userdetail")
@Tag(name = "用户信息管理",description = "用户信息管理接口")
public class UserDetailController {

    private final UserDetailService userDetailService;
    private final UserDetail userDetail;

    @Autowired
    public UserDetailController(UserDetailService userDetailService, UserDetail userDetail) {
        this.userDetailService = userDetailService;
        this.userDetail = userDetail;
    }


    /**
     * @RequestMapping:请求的映射，声明方法对应的资源名称，支持多种请求方式（post/get/delete/put）
     * @GetMapping: 以GET方式响应请求
     * @PostMapping: 以Post方式响应请求
     * @RequestParam:接收前端发送请求的时的参数名，如果和方法的参数名一致，可省略
     */
    @Operation(summary = "获取用户详情", description = "根据ID查询用户")
    @Parameters(
            {
                    @Parameter(name="id",example = "1")
            }
    )
    @GetMapping("/{id}")
    public R<UserDetail> listDetailById(@PathVariable Integer id) throws BizException {
        UserDetail userDetail = userDetailService.getById(id);
        if(userDetail.getId() == null){
            throw new BizException(ErrorMessage.USERDETAIL_NOT_FOUND);
        }
        return R.ok(userDetail);
    }


    @Operation(summary = "添加用户信息", description = "添加用户详情信息")
    @PostMapping("/add")
    public R<Boolean> add(@RequestBody UserDetailAddDto userDetailAddDto){
        BeanUtils.copyProperties(userDetailAddDto, userDetail);
        boolean save = userDetailService.save(userDetail);
        return R.ok(save);
    }

    @Operation(summary = "修改用户信息", description = "修改用户详情信息")
    @PostMapping("/update")
    public R<Boolean> modify(@RequestBody UserDetailUpdateDto userDetailUpdateDto){
        BeanUtils.copyProperties(userDetailUpdateDto, userDetail);
        boolean flag = userDetailService.updateById(userDetail);
        return R.ok(flag);
    }

    @Operation(summary = "展示所有用户信息", description = "展示所有用户详情信息")
    @GetMapping("/listAll")
    public R<Records<List<UserDetail>>> listAll(){
        IPage<UserDetail> page = new Page<>(1, 10);
        LambdaQueryWrapper<UserDetail> queryWrapper = new LambdaQueryWrapper<>();
        IPage<UserDetail> page1 = userDetailService.page(page, queryWrapper);
        Records<List<UserDetail>> pack = Records.pack(page1.getTotal(), page1.getRecords());
        return R.ok(pack);
    }

    @Operation(summary = "删除用户信息", description = "删除用户详情信息")
    @PostMapping("/delete")
    public void delete(@RequestBody UserDetailUpdateDto userDetailUpdateDto){
        BeanUtils.copyProperties(userDetailUpdateDto, userDetail);
        userDetailService.updateById(userDetail);

    }






}
